package com.group.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbMenuMapper;
import com.group.pojo.TbMenu;
import com.group.pojo.TbMenuExample;
import com.group.pojo.TbMenuExample.Criteria;
import com.group.service.MenuService;
import com.group.util.CommonUtil;

/**
 * 菜单管理service
 * @author caoyanan
 * @time 2017年10月18日下午1:09:13
 */
@Service
public class MenuServiceImpl implements MenuService{
	
	@Autowired
	private TbMenuMapper menuMapper;

	/**
	 * 设置条件
	 * @param criteria
	 * @param menu
	 */
	private void setCondition(Criteria criteria, TbMenu menu) {
		String menuStatus = menu.getMenuStatus();
		if (menuStatus != null && !"".equals(menuStatus)) {
			criteria.andMenuStatusEqualTo(menuStatus);
		}
		
		String menuName = menu.getMenuName();
		if (menuName != null && !"".equals(menuName)) {
			criteria.andMenuNameEqualTo(menuName);
		}
		
		String id = menu.getId();
		if (id != null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		Integer menuLevel = menu.getMenuLevel();
		if (menuLevel != null && !"".equals(menuLevel)) {
			criteria.andMenuLevelEqualTo(menuLevel);
		}
		
		String parentMenuId = menu.getParentMenuId();
		if (parentMenuId != null && !"".equals(parentMenuId)) {
			criteria.andParentMenuIdEqualTo(parentMenuId);
		}
	}
	@Override
	public List<TbMenu> query(TbMenu menu) {
		TbMenuExample example = new TbMenuExample();
		Criteria criteria = example.createCriteria();
		
		setCondition(criteria, menu);
		
		List<TbMenu> list = menuMapper.selectByExample(example);
		
		//按照menuLevel同一级的menuOrder进行启动排序
		list = sortMenuAsOrder(list);
		return list;
	}
	
	/**
	 * 按级别进行order排序
	 */
	public List<TbMenu> sortMenuAsOrder(List<TbMenu> list) {
		List<TbMenu> result = new LinkedList<TbMenu>();
		for (int i = 0; i < list.size(); i++) {
			TbMenu menu = list.get(i);
			InsertSort(result, menu);
		}
		return result;
	}
	/**
	 * 插入排序
	 */
	private void InsertSort(List<TbMenu> result, TbMenu menu) {
		int insertIndex = 0;
		for (; insertIndex < result.size(); insertIndex++) {
			TbMenu tbMenu = result.get(insertIndex);
			Integer newMenuLevel = menu.getMenuLevel();
			Integer curmenuLevel = tbMenu.getMenuLevel();
			if (newMenuLevel < curmenuLevel) {
				//当前级别高，肯定要放前面
				break;
			} else if (newMenuLevel > curmenuLevel) {
				//小一辈分，肯定要放后面
				continue;//后面有++
			} else {
				//辈分相同，比较order
				Integer newMenuOrder = menu.getMenuOrder();
				Integer curMenuOrder = tbMenu.getMenuOrder();
				if (newMenuOrder < curMenuOrder) {
					//order小，就放在当前的位置上
					break;
				} else {
					//order大，放后面，
					continue;//不可能出现相等的情况
				}
			}
		}
		result.add(insertIndex, menu);
	}
	@Override
	public Map<String, String> update(TbMenu menu) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");

		int update = menuMapper.updateByPrimaryKeySelective(menu);
		if (update == 1) {
			returnMap.put("success", "true");
		}
		
		return returnMap;
	}
	@Override
	public Map<String, String> updateSort(String[] menuId) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		
		TbMenu menu = new TbMenu();
		int result = 0;
		for (int i = 1; i <= menuId.length; i++) {
			menu.setMenuOrder(i);
			menu.setId(menuId[i-1]);
			int updateByPrimaryKey = menuMapper.updateByPrimaryKeySelective(menu);
			result += updateByPrimaryKey;
		}
		if (result == menuId.length) {
			returnMap.put("success", "true"); 
		}
		return returnMap;
	}
	@Override
	public String isExistSubMenu(TbMenu menu) {
		TbMenuExample example = new TbMenuExample();
		example.createCriteria().andParentMenuIdEqualTo(menu.getParentMenuId()).andMenuStatusEqualTo(menu.getMenuStatus());
		
		int counts = menuMapper.countByExample(example);
		if (counts == 0) {
			return "false";
		}
		return "true";
	}


}
